
/**
 * initialization
 */
$().ready(function(){
	// fill options (selects)
	$('.container.newBatterieForm .numList').append(
		$('<option />').attr('value', 0).html('illimité')
	)
	for (var i=1; i<=100; i++) {
		$('.container.newBatterieForm .numList').append(
			$('<option />').attr('value', i).html(i)
		);
	}
	
	// select "list" by defaut
	$('.container.newBatterieForm .fillType').removeAttr('checked');
	$('.container.newBatterieForm #filltype_list').attr('checked', 'checked');
	selectFillType();
});

function newBatterie() {
	$('.newBatterieForm input:checkbox').removeAttr('checked');
	$('.newBatterieForm input:checkbox[name="dictee_enabled"]').attr('checked', 'checked');
	
	hideContainers();
	showContainer('newBatterieForm');
}

function selectFillType() {
	var selected = $('.container.newBatterieForm input:checked[name=fillType]').val();
	
	if (selected == 'list'){
		$('#bat_pagePerSerie').val(1);
		$('#bat_questionPerPage').val(0);
		$('.container.newBatterieForm .numList').attr('disabled', 'disabled');
	} else if (selected == 'classic'){
		$('#bat_pagePerSerie').val(5);
		$('#bat_questionPerPage').val(5);
		$('.container.newBatterieForm .numList').attr('disabled', 'disabled');
	} else {
		$('#bat_pagePerSerie').val(5);
		$('#bat_questionPerPage').val(5);
		$('.container.newBatterieForm .numList').removeAttr('disabled');
	}
}

function submitNewBatterie() {
	var data = $('.newBatterieForm form.dataForm').serialize();
	if ($('.newBatterieForm #bat_pagePerSerie').attr('disabled')) {
		data += '&' + $('.newBatterieForm #bat_pagePerSerie').attr('name') + '=' + $('.newBatterieForm #bat_pagePerSerie').val();
	}
	if ($('.newBatterieForm #bat_questionPerPage').attr('disabled')) {
		data += '&' + $('.newBatterieForm #bat_questionPerPage').attr('name') + '=' + $('.newBatterieForm #bat_questionPerPage').val();
	}
	$.each($('.newBatterieForm input[type=checkbox]:not(:checked)'), function(idx, cb){
		data += '&' + $(cb).attr('name') + '=0';
	});
	
	$.ajax({
		type: 'POST',
		url: serverPath + '/batterie/post/',
		data: data,
		success: function(data) {
			loadPanel('batterie', data.id);
		},
		error: function(data) {
			displayPopup("La batterie n'a pas pu être créée.");
		},
		dataType: 'json'
	});
}
